package com.dagger.config;

import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import lombok.Getter;
import lombok.Setter;

/**
 * 消息业务配置
 * 
 * @author dagger
 *
 */

@Component
@Validated
@ConfigurationProperties(prefix = "msg")
public class MsgConfig {
	@Getter
	private static MsgConfig instance;
	
	@Getter
	@Setter
	private List<MsgBusinessConfig> configs;
	
	public boolean contains(String bussinessType) {
		if (configs == null || configs.isEmpty()) {
			return false;
		}

		for (MsgBusinessConfig config : configs) {
			if (config.getBussinessType().equals(bussinessType)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 获取一个消息的延时毫秒
	 * @param msgKey
	 * @return
	 */
	public long getDelayMillisecond(String msgKey) {
		if (configs == null || configs.isEmpty()) {
			return 0;
		}
		
		for (MsgBusinessConfig config : configs) {
			if (msgKey.startsWith(config.getBussinessType())) {
				return config.getDelayMillisecond();
			}
		}
		return 0;
	}

	public MsgBusinessConfig getConfig(String bussinessType) {
		if (configs == null || configs.isEmpty()) {
			return null;
		}

		for (MsgBusinessConfig config : configs) {
			if (config.getBussinessType().equals(bussinessType)) {
				return config;
			}
		}
		return null;
	}
	
	@PostConstruct
	public void init() {
		instance = this;
	}
}
